//: src/com/silvtar/io/StreamTransporter.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 4/21/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * Modify History:
 * Date        Version    Description
 * 4/22/2013   0.1        Create The Class
 * 4/24/2013   0.2        Redesign The Class, rewrite most of methods
 */

package com.silvtar.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.silvtar.net.NetPack;
import com.silvtar.net.Server;

/**
 * 流传输器
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.2
 */
public class StreamTransporter {

    private static StreamTransporter streamTransporter = new StreamTransporter();

    // 内存缓冲区大小
    private static int               bufferSize        = 4096;

    // ---------------------------- getter && setter ---------------------------

    public static final int getBufferSize() {

        return bufferSize;
    }

    public static final void setBufferSize(int bufferSize) {

        StreamTransporter.bufferSize = bufferSize;
    }

    // ---------------------------- 主要方法 ---------------------------

    /**
     * 将inStream输出到outputFile_S，传输完毕后会关闭inStream。
     * 
     * @param inStream
     *            输入流
     * @param outputFile_S
     *            输出文件路径
     */
    public final static void transStream(InputStream inStream,
            String outputFile_S) {

        File outputFile_F = new File(outputFile_S);
        transStream(inStream, outputFile_F);
    }

    /**
     * 将inStream输出到outputFile_F，传输完毕后会关闭inStream。
     * 
     * @param inStream
     *            输入流
     * @param outputFile_F
     *            输出文件
     */
    public static void transStream(InputStream inStream, File outputFile_F) {

        if (outputFile_F.exists()) {
            if (outputFile_F.isDirectory()) {
                StdOut.printErrLog(streamTransporter, outputFile_F.getName() + "是一个文件夹，无法被写入");
                return;
            }
        }
        OutputStream tempOutStream = null;
        try {
            tempOutStream = new FileOutputStream(outputFile_F);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        transStream(inStream, tempOutStream);
    }

    /**
     * 将inputFile_S传输到outStream，传输完毕后会关闭outStream
     * 
     * @param inputFile_S
     *            输入文件路径
     * @param outStream
     *            输出流
     */
    public final static void transStream(String inputFile_S,
            OutputStream outStream) {

        File inputFile_F = new File(inputFile_S);
        transStream(inputFile_F, outStream);
    }

    
    
    /**
     * 将inputFile_F传输到outStream，传输完毕后会关闭outStream
     * 
     * @param inputFile_F
     *            输入文件
     * @param outStream
     *            输出流
     */
    public static void transStream(File inputFile_F, OutputStream outStream) {

        if (!inputFile_F.isFile()) {
            StdOut.printErrLog(streamTransporter,
                    "The File " + inputFile_F.getName()
                            + " is a path. I can't transfer it");
            return;
        }
        InputStream tempInStream = null;
        try {
            tempInStream = new FileInputStream(inputFile_F);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        transStream(tempInStream, outStream);
    }

    /**
     * 将inStream传输到outStream 传输完毕后会关闭两个流
     * 
     * @param inputStream
     *            输入流
     * @param outputStream
     *            输出流
     */
    public static void transStream(InputStream inputStream,
            OutputStream outputStream) {

        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                outputStream);

        try {
            int length = 0;
            byte[] tByte = new byte[bufferSize];
            while ((length = bufferedInputStream.read(tByte)) > 0) {
                bufferedOutputStream.write(tByte, 0, length);
                tByte = new byte[bufferSize];
            }
            bufferedOutputStream.flush();
            
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
